/*
 *此javaCode由YMS-JDBC替换工具生成，原mapper位置com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper.RePickMapper
 */
package com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper;

import com.yonyou.iuap.yms.api.IYmsJdbcApi;
import com.yonyou.iuap.yms.param.*;
import com.yonyou.iuap.yms.param.ResultSetProcessor;
import com.yonyou.iuap.yms.processor.*;
import com.yonyou.iuap.yms.replace.CollectionUtil;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.entity.RepickStock;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.Repick.RepickDetailsDto;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.Repick.RepickListDto;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class RePickMapperImpl {
    
    @Autowired
    private IYmsJdbcApi ymsJdbcApi;

    public int Updatestorerecord(List<String> codeRfid, String recodeId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" update dxq_storerecord set dr = 1 ");
		sqlBuilder.append(" where orderId = ? ");
		parameter.addParam(recodeId);
		if (CollectionUtil.isNotEmpty(codeRfid)) {
			sqlBuilder.append(" and rfidcodeId IN ( ");
			String separator0 = " , ";
			for (int index = 0; index < codeRfid.size(); index++) {
				String id = codeRfid.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(id);
				if (index < codeRfid.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
			sqlBuilder.append(" and rfidcodeId IN ( '' )");
		}
		sqlBuilder.append(" and dr = 0 ");
		sqlBuilder.append(" and ytenant_id = ? ");
		parameter.addParam(tenant_id);

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public List<RepickListDto> GetRepickList(String orderCode, int pageBegin, int pageSize, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_ordernotice where type = 2 and auditstatus = 2 and repickstatus IN (0,1) and dr = 0 ");
		sqlBuilder.append(" and ytenant_id = ? ");
		parameter.addParam(tenant_id);
		if (orderCode != null && !orderCode.equals("")) {
			sqlBuilder.append(" and ordercode like concat('%',?,'%') ");
			parameter.addParam(orderCode);
		}
		sqlBuilder.append(" order by id ");
		sqlBuilder.append(" limit ? ,? ");
		parameter.addParam(pageBegin);
		parameter.addParam(pageSize);

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new RepickListDto());
        return (List<RepickListDto>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public int UpdateRepickStockDrStatus(String orderId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "UPDATE dxqyy_repickstock\n" + 
			         "set dr = 1              \n" + 
			         "where orderId = ?       \n" + 
			         "and dr = 0              \n" + 
			         "and ytenant_id = ?       \n";
        parameter.addParam(orderId);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int UpdateRepickStatus(List<String> codeRfid, String recodeId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" update dxqyy_repickstock set repickstatus = 1 ");
		sqlBuilder.append(" where orderId = ? ");
		parameter.addParam(recodeId);
		if (CollectionUtil.isNotEmpty(codeRfid)) {
			sqlBuilder.append(" and codeRfid IN ( ");
			String separator0 = " , ";
			for (int index = 0; index < codeRfid.size(); index++) {
				String id = codeRfid.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(id);
				if (index < codeRfid.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
			sqlBuilder.append(" and codeRfid IN ( '' )");
		}
		sqlBuilder.append(" and dr = 0 ");
		sqlBuilder.append(" and ytenant_id = ? ");
		parameter.addParam(tenant_id);

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int GetRepickListCount(String orderCode, int pageBegin, int pageSize, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select count(*) from ( ");
		sqlBuilder.append(" select * from dxq_ordernotice where type = 2 and auditstatus = 2 and repickstatus IN (0,1) and dr = 0 ");
		sqlBuilder.append(" and ytenant_id = ? ");
		parameter.addParam(tenant_id);
		if (orderCode != null && !orderCode.equals("")) {
			sqlBuilder.append(" and ordercode like concat('%',?,'%') ");
			parameter.addParam(orderCode);
		}
		sqlBuilder.append(" order by id ) as `t` ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new ColumnProcessor(Integer.class);
        return (int) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public int UpdateRepickOrderStatus(String recodeId, int repickstatus) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "UPDATE dxq_ordernotice                     \n" + 
			         "set repickstatus = ?                       \n" + 
			         "where orderid = ? and dr = 0 and `type` = 2\n";
        parameter.addParam(repickstatus);
		parameter.addParam(recodeId);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int InsertRepickStock(List<RepickStock> repickStockList) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" insert into dxqyy_repickstock(coderfid, RfidCode, product_id, productname, locationID, locationName, product_sku_id, ");
		sqlBuilder.append(" orderId, product_sku_name, createor, labelcount, ordercode,orderDetailId,ytenant_id) ");
		sqlBuilder.append(" values ");
		if (CollectionUtil.isNotEmpty(repickStockList)) {
			String separator0 = " , ";
			for (int index = 0; index < repickStockList.size(); index++) {
				RepickStock item = repickStockList.get(index);
				sqlBuilder.append(" (?,?,?,?,?,?,?,? ");
				parameter.addParam(item.getCoderfid());
				parameter.addParam(item.getRfidCode());
				parameter.addParam(item.getProductId());
				parameter.addParam(item.getProductname());
				parameter.addParam(item.getLocationId());
				parameter.addParam(item.getLocationName());
				parameter.addParam(item.getProductSkuId());
				parameter.addParam(item.getOrderId());
				sqlBuilder.append(" ,?,?,?,?,?,?) ");
				parameter.addParam(item.getProductSkuName());
				parameter.addParam(item.getCreateor());
				parameter.addParam(item.getLabelCount());
				parameter.addParam(item.getOrderCode());
				parameter.addParam(item.getOrderDetailId());
				parameter.addParam(item.getTenant_id());
				if (index < repickStockList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
		}

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public List<RepickStock> GetRepickCountByStatus(String orderId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select *              \n" + 
			         "from dxqyy_repickstock\n" + 
			         "where orderId = ?     \n" + 
			         "AND dr = 0            \n" + 
			         "AND ytenant_id = ?     \n" +
			         "AND repickstatus = 1; \n";
        parameter.addParam(orderId);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new RepickStock());
        return (List<RepickStock>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<RepickStock> RepickDetailByOrderIdAndPId(String orderId, String orderdetailid, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select sr.id            as storerecodeId,          \n" + 
			         "sr.rfidcodeId    as coderfid,                      \n" + 
			         "cr.rfid_code     as rfidCode,                      \n" + 
			         "cr.location_id   as locationId,                    \n" + 
			         "dl.locationName  as locationName,                  \n" + 
			         "dl.warehouse_id  as warehouseId,                   \n" + 
			         "cr.f_stock_count as labelCount                     \n" + 
			         "from dxq_storerecord as sr                         \n" + 
			         "left join dxq_coderfid cr on sr.rfidcodeId = cr.id \n" + 
			         "left join dxq_location dl on cr.location_id = dl.id\n" + 
			         "where sr.dr = 0                                    \n" + 
			         "and sr.orderid = ?                                 \n" + 
			         "and sr.orderdetailid = ?                           \n" + 
			         "and sr.ordertype = 2                               \n" + 
			         "and sr.ytenant_id = ?                               \n";
        parameter.addParam(orderId);
		parameter.addParam(orderdetailid);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new RepickStock());
        return (List<RepickStock>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<RepickDetailsDto> GetRepickDetails(String orderId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select rep.ID             as id,                                    \n" + 
			         "rep.coderfid       as coderfid,                                     \n" + 
			         "rep.RfidCode       as RfidCode,                                     \n" + 
			         "rep.product_id     as productId,                                    \n" + 
			         "p.productName      as productname,                                  \n" + 
			         "p.code             as productCode,                                  \n" + 
			         "rep.locationID     as locationId,                                   \n" + 
			         "rep.locationName   as locationName,                                 \n" + 
			         "dl2.warehouse_id   as warehouseId,                                  \n" + 
			         "rep.product_sku_id as productSkuId,                                 \n" + 
			         "pst.code           as productSkuCode,                               \n" + 
			         "pst.skuName        as productSkuName,                               \n" + 
			         "rep.orderId        as orderId,                                      \n" + 
			         "rep.orderCode      as orderCode,                                    \n" + 
			         "rep.remark         as remark,                                       \n" + 
			         "rep.repickstatus   as repickstatus,                                 \n" + 
			         "rep.labelCount     as labelCount,                                   \n" + 
			         "dc.location_id     as NlocationId,                                  \n" + 
			         "dl.locationName    as Nlocationname,                                \n" + 
			         "dl.warehouse_id    as NwarehouseId                                  \n" + 
			         "from dxqyy_repickstock as rep                                       \n" + 
			         "LEFT JOIN dxq_coderfid dc on rep.coderfid = dc.id                   \n" + 
			         "LEFT JOIN dxqyy_product p on rep.product_id = p.id                  \n" + 
			         "LEFT JOIN dxqyy_productskutemp as pst on rep.product_sku_id = pst.id\n" + 
			         "LEFT JOIN dxq_location dl on dl.id = dc.location_id                 \n" + 
			         "LEFT JOIN dxq_location dl2 on rep.locationID = dl2.id               \n" + 
			         "where rep.orderId = ?                                               \n" + 
			         "AND rep.dr = 0                                                      \n" + 
			         "AND rep.ytenant_id = ?                                               \n" +
			         "order by rep.ID;                                                    \n";
        parameter.addParam(orderId);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new RepickDetailsDto());
        return (List<RepickDetailsDto>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }

}
